package com.fk.reggie.interceptor;

import com.alibaba.fastjson.JSON;
import com.fk.reggie.common.BaseContext;
import com.fk.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("拦截到的请求是:{}", request.getRequestURI());
        Long employee = (Long) request.getSession().getAttribute("employee");
        Long user = (Long) request.getSession().getAttribute("user");
        if (employee != null) {
            BaseContext.setThreadLocal(employee);
            return true;
        }

        if (user != null) {
            BaseContext.setThreadLocal(user);
            return true;
        }


        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        return false;

    }
}
